package com.k4rthik.clipall.ui;

import com.k4rthik.clipall.clipb.OwnershipClipboardListener;
import com.k4rthik.clipall.commons.Level;
import com.k4rthik.clipall.commons.Logger;
import com.k4rthik.clipall.commons.PropertyHandler;
import com.k4rthik.clipall.network.Connections;
import com.k4rthik.clipall.network.ShareServer;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Perseus
 * Date: 7/4/12
 * Time: 4:35 PM
 */

public class Main
{
    public static void main(String[] args)
    {
        PropertyHandler.init();
        Thread networkConnections = new Connections();
        networkConnections.start();

        SecurityManager securityManager = System.getSecurityManager();
        if (securityManager != null) {
            try {
                securityManager.checkSystemClipboardAccess();
            } catch (Exception e) {
                Logger.log("Program cannot access System clipboard. Exiting.", Level.FATAL);
                System.exit(1);
            }
        } else {
            //Start Ownership-based Clipboard Polling on supported systems
            Thread clipB = new OwnershipClipboardListener();
            clipB.start();

            //Start Content-based Clipboard Polling on supported systems
            //Thread legacyClipB = new PollingClipboardListener();
            //legacyClipB.start();
        }

        if(args.length > 0 && args[0].equalsIgnoreCase("S")) {
            Logger.log("Starting in server mode", Level.INFO);
            try {
                ShareServer shareServer = new ShareServer();
                shareServer.start();
            } catch (IOException e) {
                Logger.log("Unable to start server", e);
            }
        }

        MainWindowPanel.build();
    }
}
